package com.zzl.study.auth.controller;

import com.github.pagehelper.PageInfo;
import com.zzl.study.auth.domain.Response;
import com.zzl.study.auth.domain.dto.Options;
import com.zzl.study.auth.domain.dto.RegisteredClientDto;
import com.zzl.study.auth.enums.AuthorizationGrantType;
import com.zzl.study.auth.enums.ClientAuthenticationMethod;
import com.zzl.study.auth.service.RegisteredClientService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: zhangzl
 * @date: 2023/12/8 10:43
 * @version: 1.0
 * @description: 注册客户端Controller
 */
@Slf4j
@RestController
@RequestMapping("/client")
public class RegisteredClientController {

    @Resource
    private RegisteredClientService service;

    /**
     * 获取授权模式下拉框集合
     *
     * @return 授权模式下拉框集合
     */
    @GetMapping("/getAuthorizationGrantTypes")
    public Response<List<Options>> getAuthorizationGrantTypes() {
        List<Options> optionsList = AuthorizationGrantType.toList();
        return Response.success(optionsList);
    }

    /**
     * 获取客户端认证方法下拉框集合
     *
     * @return 客户端认证方法下拉框集合
     */
    @GetMapping("/getClientAuthenticationMethods")
    public Response<List<Options>> getClientAuthenticationMethods() {
        List<Options> optionsList = ClientAuthenticationMethod.toList();
        return Response.success(optionsList);
    }

    /**
     * 查询客户端分页集合
     *
     * @param clientName 客户端名称
     * @param pageNum    当前页
     * @param pageSize   每页条数
     * @return 客户端分页集合
     */
    @GetMapping("/selectPageList")
    public Response<PageInfo<RegisteredClientDto>> selectPageList(@RequestParam(value = "clientName", required = false) String clientName,
                                                                  @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        try {
            PageInfo<RegisteredClientDto> pageInfo = this.service.selectRegisteredClientPageList(clientName, pageNum, pageSize);
            return Response.success(pageInfo);
        } catch (Exception e) {
            log.error("查询注册客户端分页集合出错!", e);
            return Response.error(500, e.getMessage(), null);
        }
    }

    /**
     * 创建客户端
     *
     * @param registeredClientDto 客户端对象
     * @return 客户端密钥
     */
    @PostMapping("/create")
    public Response<String> create(@RequestBody RegisteredClientDto registeredClientDto) {
        try {
            String clientSecret = this.service.createRegisteredClient(registeredClientDto);
            log.info("客户端 {} 注册成功!", registeredClientDto.getClientName());
            return Response.success(clientSecret);
        } catch (Exception e) {
            log.error("客户端 {} 注册失败!", registeredClientDto.getClientName(), e);
            return Response.error(500, e.getMessage(), null);
        }
    }

    /**
     * 根据ID查询客户端
     *
     * @param id ID
     * @return 客户端
     */
    @GetMapping("/selectById/{id}")
    public Response<RegisteredClientDto> selectById(@PathVariable("id") String id) {
        try {
            RegisteredClientDto registeredClientDto = this.service.selectRegisteredClientById(id);
            return Response.success(registeredClientDto);
        } catch (Exception e) {
            log.error("根据ID {} 查询注册客户端!", id, e);
            return Response.error(500, e.getMessage(), null);
        }
    }

    /**
     * 更新客户端
     *
     * @param registeredClientDto 客户端对象
     * @return 是否成功
     */
    @PostMapping("/update")
    public Response<Boolean> update(@RequestBody RegisteredClientDto registeredClientDto) {
        try {
            this.service.updateRegisteredClient(registeredClientDto);
            log.info("客户端 {} 更新成功!", registeredClientDto.getClientName());
            return Response.success(true);
        } catch (Exception e) {
            log.error("客户端 {} 更新失败!", registeredClientDto.getClientName(), e);
            return Response.error(500, e.getMessage(), false);
        }
    }

    /**
     * 批量删除客户端
     *
     * @param ids ID数组
     * @return 是否成功
     */
    @DeleteMapping("/deleteBatch")
    public Response<Boolean> deleteBatch(@RequestBody String[] ids) {
        try {
            this.service.deleteRegisteredClientByIds(ids);
            return Response.success(true);
        } catch (Exception e) {
            log.error("批量删除客户端失败!", e);
            return Response.error(500, e.getMessage(), false);
        }
    }

}
